import asyncio
import aiohttp
import aiofiles

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/91.0.4472.106 Safari/537.36'
}

url_list = [
    'https://kr.shanghai-jiuxin.com/file/mm/20210503/0yuonplsphe.jpg',
    'https://kr.shanghai-jiuxin.com/file/mm/20210503/f0qbuophvkr.jpg',
    'https://kr.wzh3c.com/file/mm/20210503/2vvm2glxboz.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/y200jppiq5e.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/yznbw3e5pnm.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/5diccg3omln.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/ucevkb3sn2e.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/kua4w1hpmjf.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/2a2uyjlbdaa.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/e5njd1mfijx.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/4wzieuu3tr2.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/as1tovmnmze.jpg',
    'http://kr.shanghai-jiuxin.com/file/mm/20210503/2tjlmdtz531.jpg'
]


async def download_img(path, img_src):
    name = img_src.rsplit('/', 1)[1]
    img_path = path + '/' + name
    print(f'开始下载: {name}, 地址: {img_src}, 图片地址: {img_path}')
    async with aiohttp.ClientSession() as session:
        async with session.get(img_src) as resp:
            img_data = await resp.content.read()
            async with aiofiles.open(img_path, mode='wb') as fp:
                await fp.write(img_data)

    print(name, "下载成功")

def get_src():
    path = 'D:/ImgTest'
    tasks = []
    for src in url_list:
        tasks.append(download_img(path, src))
    asyncio.run(asyncio.wait(tasks))


if __name__ == '__main__':
    get_src()